<?php use_javascript('/js/json_parse.js') ?>
<?php include_partial('jsSelectDemandeurClient', array('form' => $form, 'demandeur' => $demandeur, 'objDef' => $objDef)) ?>

<script type="text/javascript">

function addNnis(){
	varListeNNIs = document.getElementById('demandes1_nniClient').value;
	getXmlHttpRequest();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			tableauClients = xhr.responseText;
			document.getElementById('listeClients').innerHTML = tableauClients;
		}
	};
	xhr.open("POST","<?php print url_for('ajax/addClients'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("listeNNIs="+varListeNNIs);
}
function removeNNI(nni){
	getXmlHttpRequest();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			tableauClients = xhr.responseText;
			document.getElementById('listeClients').innerHTML = tableauClients;
		}
	};
	xhr.open("POST","<?php print url_for('ajax/removeClients'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("NNI="+nni);
}
</script>
<?php $module = 'demandes'?>
<?php $className = 'groupedemandes'?>
<form id="myForm" action="<?php print url_for('demandes/submitClients'); ?>" method="post" tmt:validate = "true" tmt:callback="errorOnLabels" class="defaultForm">
        <fieldset><legend><?php echo "$nomType / $nomCategorie" ?></legend>
		<?php if (!$form->getObject()->isNew()): ?>
		<input type="hidden" name="sf_method" value="put" />
		<?php endif; ?>
		<?php echo $form->renderGlobalErrors() ?>
			<?php  if ($form->isCSRFProtected()) : ?> 
			  <?php echo $form['_csrf_token']; ?> 
			<?php endif; ?>							
			<?php echo $form['id']; ?>
		  <?php $util = new Util();
	   	  $objDef = $util->getOEDef($className);
	   	  $section = '';
		  foreach($objDef['attributes'] as $fieldName => $att):?>
		    <?php if($att['type']=='BO'){$fieldName = $fieldName.'_id';}?>
	        <?php if($section != $att['section']):?>
	        	<?php if($section !=''):?>
	        		</fieldset>
	        	<?php endif;?>
	            <fieldset><legend><?php echo $att['section']?></legend>
	  		<?php endif;?>
	  		<?php if($fieldName=='demandeur_id'):?>
					<table>
				    <tr>
				      <th>NNI du demandeur :</th>
				      <td><?php echo $demandeur->nni ?></td>
				    </tr>
	                <tr>
				      <th>Nom :</th>
				      <td><?php echo $demandeur->nom ?></td>
				    </tr>
	                <tr>
				      <th>Pr&eacute;nom :</th>
				      <td><?php echo $demandeur->prenom ?></td>
				    </tr>
				    <tr>
				      <th>E-mail du demandeur :</th>
				      <td><?php echo $demandeur->email ?></td>
				    </tr>
				    </table>
	  		<?php endif;?>
	  		<?php if($fieldName=='nniClient'):?>
	  			
	  		<?php endif;?>
		    <div id="wrapper<?php echo $fieldName?>">
		  	<?php if($att['show']):?>	
			  	<?php echo $form[$fieldName]->renderRow() ?>
			  	<?php if($att['type']=='file'):?>
				  <?php include_partial('renderFile', array('self' => $form->getObject(),'fieldName' => $fieldName, 'label' => $att['label'], 'dirOrigin' => sfConfig::get('sf_upload_dir'))) ?>
				<?php endif;?> 
		  	<?php else:?>
    	   	    <?php echo $form[$fieldName]->render() ?>
		  	 <?php endif;?>	
       	    <?php if($fieldName=='nniClient'):?>
       	    	<h1>Pour ajouter des clients dans la liste, vous devez saisir les NNI dans le champ <i>"NNI(s) Client"</i>, et ensuite cliquer sur l'ic&ocirc;ne. 
       	    	<a href="javascript:addNnis();"><img src="/../images/formulaires/arrow_refresh.png" title="Cliquez ici pour ajouter le(s) NNI &agrave; la liste"/></a></h1>
       	    <?php endif;?>
		    </div>
	  		<?php if($fieldName=='nniClient'):?>
	  			<div id="listeClients">
	  			<span id="nbrClients" style="display: none;">0</span></div>	
	  		<?php endif;?>
		    
	  		<?php $section = $att['section']?>
		  <?php endforeach;?>
		  </fieldset>
		<div class="buttons">
          <?php echo link_to( '<img title="Retour" src="/images/formulaires/retour.png"></img>', 'demandes/demandesPhase2' )?>
          <?php echo link_to( '<img title="Cancel" src="/images/formulaires/annuler.png"></img>', 'demandes/anuler' )?>
          <input name="Submit" title="<?php echo __('Apply')?>" type="image" src="/images/formulaires/valider.png">
        </div>
        <a id="addNotif"  onclick="showForm('participant_notifier', 'participants_notifier', 'new');" href="#">
		<img title="<?php echo __('Clic here to add other people');?>" src="/images/icons/16/add_people.png"/></a> <?php echo __('Add other people');?>
		<div id ="list_participants_notifier" class="" ></div>
            
</fieldset>
</form>


<div id ="participants_notifierForm" class="editItemForm" style="display:none"></div>
<div id ="addparticipant_notifierTarget" class="editItemForm" style="display:none"></div>

<script type="text/javascript">
//$(document).ready(function(){    
	renderList("participant_notifier", "participants_notifier" ,0);
	addNnis();
//});
</script>

<script type="text/javascript">
	//function getNbrClients(){
	//	getXmlHttpRequest();
	//	xhr.onreadystatechange = function(){
	//		if(xhr.readyState == 4 && xhr.status == 200)
	//			{
	//			nbrClients = xhr.responseText;
	//			return nbrClients;
	//		}
	//	};
	//	xhr.open("POST","<?php print url_for('ajax/getNbrClients'); ?>",true);
	//	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//	xhr.send();
	//}

	function customVal(validators){
		var qCli;
		qCli = parseInt(document.getElementById('nbrClients').innerHTML);
		if(qCli==0){
			validators.push(tmt.validator.fieldValidatorFactory(document.getElementById('demandes1_nniClient')));
		}
		varMax = parseInt("<?php echo sfConfig::get('app_dmdGroupee_nbMaxClients',30)  ?>");
		if(qCli>varMax){
			x = validators.length;
			validators.push(tmt.validator.fieldValidatorFactory(document.getElementById('demandes1_nniClient')));
			validators[x].message = "Le nombre maximal des clients dans une demande group\351e est " + varMax;
		}  
	}
		
</script>